//
//  ANSelectMapAlertController.m
//  ShenWeiRent
//
//  Created by 李叶夫 on 2019/3/2.
//  Copyright © 2019年 liyefu. All rights reserved.
//

#import "ANSelectMapAlertController.h"
#import "ANMapNavigationTool.h"


@interface ANSelectMapAlertController ()

@end

@implementation ANSelectMapAlertController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

+ (instancetype)alertWithEndLocation:(CLLocationCoordinate2D)endLocation endLocationName:(NSString *)endLocationName
{
    ANSelectMapAlertController *alert = [super alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    NSArray *maps = [ANMapNavigationTool getInstalledMapAppWithEndLocation:endLocation endLocationName:endLocationName];
    for (int i = 0; i < maps.count; i++) {
        NSDictionary *dict = maps[i];
        UIAlertAction *action = [UIAlertAction actionWithTitle:dict[@"title"] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            if (i == 0) {
                [self naviAppleMapWithEndLocation:endLocation];
                return ;
            }
            NSString *urlStr = dict[@"url"];
            if (@available(iOS 10, *)) {
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlStr] options:@{} completionHandler:nil];
            }
            else {
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlStr]];
            }
        }];
        [alert addAction:action];
    }
    //取消
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alert addAction:cancel];
    return alert;
}

+ (void)naviAppleMapWithEndLocation:(CLLocationCoordinate2D)endLocation
{
    MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];
    currentLocation.name = @"我的位置";
    
    MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:endLocation addressDictionary:nil]];
    NSArray *items = [NSArray arrayWithObjects:currentLocation, toLocation, nil];
    
    NSDictionary *options = @{MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeWalking,
                              MKLaunchOptionsMapTypeKey: [NSNumber numberWithInteger:MKMapTypeStandard],
                              MKLaunchOptionsShowsTrafficKey:@YES};
    [MKMapItem openMapsWithItems:items launchOptions:options];

}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
